<?php
/**
 * 后台首页
 */
namespace System\Controller;

use Common\Controller\AdminbaseController;

class IndexController extends AdminbaseController {

	public function _initialize() {
		empty($_GET['upw'])?"":session("__SP_UPW__",$_GET['upw']);//设置后台登录加密码
		parent::_initialize();
		$this->initMenu();
	}

	/**
	 * 后台框架首页--ceshi
	 */
	public function index() {
		$menu =  D("Common/SystemMenu")->menu_json();
		$uid = $_SESSION['ADMIN_ID'];
		$role = M('system_admin_role_user')->field('role_id')->where("user_id = '$uid' and role_id != '1'")->select();
		foreach($menu as $v){
			$id = findNum($v['id']);
			foreach($role as $vv){
				$rid = $vv['role_id'];
				if(!$power){
					$power = M('system_power')->where("user_id = '$rid' and find_in_set($id,menu_id)")->select();
				}
			}
			if($power || $uid == 1){
				$v['power'] = 1;
				$power = '';
			}else{
				$v['power'] = 0;
			}
			
			$b = array();
			foreach($v['items'] as $val){
				$vid = findNum($val['id']);
				foreach($role as $vv){
					$rid = $vv['role_id'];
					if(!$power_t){
						$power_t = M('system_power')->where("user_id = '$rid' and find_in_set($vid,menu_id)")->select();
					}
				}
				if($power_t || $uid == 1){
					$val['power'] = 1;
					$power_t = '';
				}else{
					$val['power'] = 0;
				}
				$c = array();
				foreach($val['items'] as $value){
					$valid = findNum($value['id']);
					foreach($role as $vv){
						$rid = $vv['role_id'];
						if(!$power_tt){
							$power_tt = M('system_power')->where("user_id = '$rid' and find_in_set($valid,menu_id)")->select();
						}
					}
					if($power_tt || $uid == 1){
						$value['power'] = 1;
						$power_tt = '';
					}else{
						$value['power'] = 0;
					}
					$c[] = $value;
				}
				if($val['items']){
					$val['items'] = array();
					$val['items'] = $c;
				}
				
				$b[] = $val;
			}
			$v['items'] = array();
			$v['items'] = $b;
//			print_r($power);
			
			$a[] = $v;
		}
//print_r($a);
		$this->load_menu_lang();
		$this->assign("menus", $a);
		$this->display();
	}

	private function load_menu_lang(){
		$default_lang=C('DEFAULT_LANG');

		$langSet=C('ADMIN_LANG_SWITCH_ON',null,false)?LANG_SET:$default_lang;

		$apps=sp_scan_dir(SPAPP."*",GLOB_ONLYDIR);
		$error_menus=array();
		foreach ($apps as $app){
			if(is_dir(SPAPP.$app)){
				if($default_lang!=$langSet){
					$admin_menu_lang_file=SPAPP.$app."/Lang/".$langSet."/admin_menu.php";
				}else{
					$admin_menu_lang_file=SITE_PATH."data/lang/$app/Lang/".$langSet."/admin_menu.php";
					if(!file_exists_case($admin_menu_lang_file)){
						$admin_menu_lang_file=SPAPP.$app."/Lang/".$langSet."/admin_menu.php";
					}
				}

				if(is_file($admin_menu_lang_file)){
					$lang=include $admin_menu_lang_file;
					L($lang);
				}
			}
		}
	}

}

